package com.uroad.jilinpda.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.uroad.jilinpda.R;
import com.uroad.jilinpda.modle.IssueMDL;
import com.uroad.jilinpda.widget.SYSDiaLogUtils;

import java.util.ArrayList;
import java.util.List;

public class TobeActivatedAdapter extends RecyclerView.Adapter<TobeActivatedAdapter.VH>{
    private List<IssueMDL.Data> mDatas = new ArrayList<>();
    private Activity context;
    private View mFooterView;
    public static final int TYPE_FOOTER = 1;
    public static final int TYPE_NORMAL = 2;
    private onItemClickListener clickListener;
    private ItemCancel itemCancel;

    public TobeActivatedAdapter(Activity context){
        this.context = context;
    }
    @Override
    public TobeActivatedAdapter.VH onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mFooterView != null && viewType == TYPE_FOOTER){
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_footer1, parent, false);
            return new TobeActivatedAdapter.VH(v);
        }
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tobe_activate, parent, false);
        return new TobeActivatedAdapter.VH(v);
    }

    public void setItemCancel(ItemCancel itemCancel){
        this.itemCancel = itemCancel;
    }
    @Override
    public void onBindViewHolder(TobeActivatedAdapter.VH holder, @SuppressLint("RecyclerView") int position) {
        if(getItemViewType(position) == TYPE_NORMAL){
            holder.tv_plate_number.setText("车牌号："+mDatas.get(position).getNumberplate());
            holder.tv_plate_color.setText("车牌颜色："+mDatas.get(position).getName());
            holder.tv_time.setText(mDatas.get(position).getCompletetime());
            holder.tv_zhuangtai.setText(mDatas.get(position).getCheckstatuss());
            if (mDatas.get(position).getOrder_status().equals("1")){
                holder.btn_getCode.setVisibility(View.VISIBLE);
            }else {
                holder.btn_getCode.setVisibility(View.GONE);
            }
            if (mDatas.get(position).getCardno() != null && !mDatas.get(position).getCardno().equals(""))
                holder.tv_card_number.setText("ETC卡号："+mDatas.get(position).getCardno());


            if (mDatas.get(position).getCheckstatus().equals("4")){
                holder.bt_cancel.setVisibility(View.VISIBLE);
                holder.bt_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        itemCancel.OnCancel(mDatas.get(position).getOrderno());
                    }
                });
            }
            holder.btn_getCode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDatas.get(position).getOrder_status().equals("1")) {
                        itemCancel.OngetCode(mDatas.get(position).getOrderno(),mDatas.get(position).getNumberplate());
                    }else {
                        return;
                    }

                }
            });
            if (!TextUtils.isEmpty(mDatas.get(position).getPlatmsg())){
                holder.tv_failure_cause.setVisibility(View.VISIBLE);
                holder.tv_failure_cause.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        SYSDiaLogUtils.showOnlyTextDialog(context,"失败原因",mDatas.get(position).getPlatmsg());
                    }
                });
            }else{
                holder.llRootView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (clickListener != null) {
                            clickListener.onItemClick(position);
                        }
                    }
                });
            }
        }else if (getItemViewType(position) == TYPE_FOOTER){
            holder.tv_footer.setText("加载更多数据。。。");
        }

    }

    public void setOnItemClickListener(onItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setmDatas(List<IssueMDL.Data> mDatas){
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        if(mFooterView != null){
            return mDatas.size() + 1;
        }
        return mDatas.size();
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount()-1);
    }

    public View getFooterView() {
        return mFooterView;
    }

    public void offFooterView(){
        mFooterView = null;
    }
    @Override
    public int getItemViewType(int position) {
        if (mFooterView == null){
            return TYPE_NORMAL;
        }
        if (position == getItemCount()-1){
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }

    public class VH extends RecyclerView.ViewHolder{
        public TextView tv_plate_number;
        public TextView tv_plate_color;
        private TextView tv_footer;
        private TextView tv_time;
        private TextView tv_zhuangtai;
        private TextView tv_card_number;
        private LinearLayout llRootView;
        private Button bt_cancel;
        private Button btn_getCode;
        private TextView tv_failure_cause;
        public VH(View v) {
            super(v);
            if (itemView == mFooterView){
                return;
            }
            tv_footer = (TextView) v.findViewById(R.id.tv_footer);
            tv_time = (TextView) v.findViewById(R.id.tv_time);
            tv_zhuangtai = (TextView) v.findViewById(R.id.tv_zhuangtai);
            tv_card_number = (TextView) v.findViewById(R.id.tv_card_number);
            tv_plate_number = (TextView) v.findViewById(R.id.tv_plate_number);
            tv_plate_color = (TextView) v.findViewById(R.id.tv_plate_color);
            llRootView = (LinearLayout) v.findViewById(R.id.llRootView);
            bt_cancel = (Button)v.findViewById(R.id.bt_cancel);
            btn_getCode = (Button)v.findViewById(R.id.btn_getCode);
            tv_failure_cause = (TextView) v.findViewById(R.id.tv_failure_cause);
        }
    }

    public interface onItemClickListener{
        void onItemClick(int position);
    }

    public interface ItemCancel {
        public void OnCancel(String orderno);
        public void OngetCode(String orderno, String plateNumber);
    }
}
